#ifndef SOCKETHANDLER_H
#define SOCKETHANDLER_H

#include <map>
#include <list>

#include "socket_include.h"
#include "ISocketHandler.h"


class SocketHandler : public ISocketHandler
{
protected:
	/** Map type for holding file descriptors/socket object pointers. */
	typedef std::map<SOCKET,Socket *> socket_m;

public:
	/** SocketHandler constructor. */
	SocketHandler();

	~SocketHandler();

	/** Add socket instance to socket map. Removal is always automatic. */
	void Add(Socket *);

	/** Get status of read/write/exception file descriptor set for a socket. */
	void Get(SOCKET s,bool& r,bool& w);

	/** Wait for events, generate callbacks. */
	int Select(long sec,long usec, SelectMode mode = RWMode);

	/** This method will not return until an event has been detected. */
	int Select(SelectMode mode = RWMode);

	/** Wait for events, generate callbacks. */
	int Select(struct timeval *tsel, SelectMode mode = RWMode);

	/** Check that a socket really is handled by this socket handler. */
	bool Valid(Socket *);

	/** Return number of som_maxsockckets handled by this handler.  */
	size_t GetCount() { return m_sockets.size();}
private:
	/** Remove socket from socket map, used by Socket class. */
	void Remove(Socket *);
	socket_m m_sockets; ///< Active sockets map
	SOCKET m_maxsock; ///< Highest file descriptor + 1 in active sockets list
	fd_set m_rfds; ///< file descriptor set monitored for read events
	fd_set m_wfds; ///< file descriptor set monitored for write events
};

#endif //SOCKETHANDLER_H
